package com.zrrd.codeStream.copy;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author WanJl
 * @version 1.0
 * @title Copy01
 * @description 字节输入-字节输出-复制
 * @create 2024/5/19
 */
public class Copy02 {
    /**
     * 复制
     * @param starPath 起始文件路径
     * @param destPath 目标文件路径
     */
    public static void copy(String starPath, String destPath) {
        //创建对象
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            //创建对象
            fis=new FileInputStream(starPath);
            fos=new FileOutputStream(destPath);

            byte[] buffer=new byte[1024];
            //读取并且写入
            int bytesRead=0;
            while ((bytesRead=fis.read(buffer))!=-1){//每次读取1个KB
                //写入
                fos.write(buffer,0,bytesRead);
            }


        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                fis.close();
                fos.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }


    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        copy("E:\\01我的文件夹\\JT商城后台管理系统.png", "D:/AAAA.png");
        long end = System.currentTimeMillis();
        System.out.println(end - start);
    }
}
